<article id="wikiArticle">
<div></div>
<h2 id="信息">信息</h2>
<pre><code class="language-javascript">SyntaxError: Expected ';' (Edge)</code></pre>
<pre><code class="language-javascript"> SyntaxError: missing ; before statement (Firefox)
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>SyntaxError</code></a>.</p>
<h2 id="哪里出错了">哪里出错了?</h2>
<p>某个地方丢失了一个(<code>;</code>)。 <a href="/en-US/docs/Web/JavaScript/Reference/Statements">JavaScript 语句</a>必须以分号结束。 有一些是由 <a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion">自动分号插入(ASI)</a> 的使用所影响到的， 但在这种情况下，你需要提供一个分号，以便于JavaScript可以将源代码解析正确。然而，通常情况下，这个错误只是另一个错误一个导致的，如不正确转义字符串，使用var的错误。你也可能有太多的括号的地方。当出现此错误时，仔细检查语法。</p>
<h2 id="例子">例子</h2>
<p> </p>
<p>转义字符串</p>
<p>这个错误可能发生不正确时容易转义字符串JavaScript引擎是希望你的字符串的末尾已经。例如:</p>
<pre><code class="language-js example-bad">var foo = 'Tom's bar';
// SyntaxError: missing ; before statement</code></pre>
<p>你可以用双引号，或者用\转义：</p>
<pre><code class="language-js example-good">var foo = "Tom's bar";
var foo = 'Tom\'s bar';
</code></pre>
<h3 id="用var声明属性">用var声明属性</h3>
<p>你不能将带有变量声明的对象或数组的属性用<code>var</code>来声明。</p>
<pre><code class="language-js example-bad">var obj = {};
var obj.foo = "hi"; // SyntaxError missing ; before statement

var array = [];
var array[0] = "there"; // SyntaxError missing ; before statement
</code></pre>
<p>而是省略掉<code>var</code>关键词：</p>
<pre><code class="language-js example-good">var obj = {};
obj.foo = "hi";

var array = [];
array[0] = "there";
</code></pre>
<h3 id="不推荐使用的关键字">不推荐使用的关键字</h3>
<p>如果你用的是另一种编程语言，那么在javaScript中使用不具有相同或完全没有意义的关键字也是很常见的：</p>
<pre><code class="language-js example-bad line-numbers language-js"><code class="language-js">def <span class="function token">print</span><span class="punctuation token">(</span>info<span class="punctuation token">)</span><span class="punctuation token">{</span>
  console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>info<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span><span class="punctuation token">;</span> <span class="comment token">// SyntaxError missing ; before statement</span></code></code></pre>
<p>因此，建议使用<code>function</code>而不是<code>def</code>：</p>
<pre><code class="language-js example-good line-numbers language-js"><code class="language-js"><span class="keyword token">function</span> <span class="function token">print</span><span class="punctuation token">(</span>info<span class="punctuation token">)</span><span class="punctuation token">{</span>
  console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>info<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span><span class="punctuation token">;</span></code></code></pre>
<p> </p>
<h2 id="参考">参考</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion">自动分号插入(ASI)</a> </li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Statements">JavaScript 语句</a></li>
</ul>
</article>